Completed
Branch master (ebb499)
by Alexey
04:15
created

Inji.start   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
/**
2
 * Inji js core
3
 *
4
 * @author Alexey Krupskiy <[email protected]>
5
 * @link http://inji.ru/
6
 * @copyright 2015 Alexey Krupskiy
7
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
8
 */
9
10
function Inji() {
11
  this.options = {};
12
  this.onLoadCallbacks = [];
13
  this.loaded = false;
14
  this.listeners = {};
15
  this.loadedScripts = {};
16
}
17
Inji.prototype.onLoad = function (callback, start) {
18
  if (typeof callback == 'function') {
19
    if (this.loaded) {
20
      callback();
21
    } else {
22
      if (start) {
23
        this.onLoadCallbacks.unshift(callback);
24
      } else {
25
        this.onLoadCallbacks.push(callback);
26
      }
27
    }
28
  }
29
}
30
Inji.prototype.startCallbacks = function () {
31
  console.log('inji start onload');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
32
  while (callback = this.onLoadCallbacks.shift()) {
0 ignored issues
show
Bug introduced by
The variable callback seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.callback.
Loading history...
33
    if (typeof callback == 'function') {
34
      callback();
35
    }
36
  }
37
  if (this.onLoadCallbacks.length != 0) {
38
    this.startCallbacks();
39
  }
40
  var indicator = document.getElementById('loading-indicator');
41
  if (indicator) {
42
    indicator.style.display = 'none';
43
  }
44
  inji.loaded = true;
45
  console.log('inji start complete');
46
}
47
Inji.prototype.start = function (options) {
48
  console.log('Inji start');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
49
  for (key in options.compresedScripts) {
0 ignored issues
show
Bug introduced by
The variable key seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.key.
Loading history...
50
    inji.loadedScripts[options.compresedScripts[key]] = true;
51
  }
52
  this.options = options;
53
  if (options.onLoadModules) {
54
    this.onLoad(function () {
55
      for (key in options.onLoadModules) {
0 ignored issues
show
Bug introduced by
The variable key seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.key.
Loading history...
56
        if (typeof inji[key] == 'undefined') {
57
          inji[key] = new window[key]();
58
          if (typeof (inji[key].init) == 'function') {
59
            console.log(key + ' init');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
60
            inji[key].init();
61
          }
62
        }
63
      }
64
    }, true)
65
  }
66
  if (options.scripts.length > 0) {
67
    this.loadScripts(options.scripts, 0);
68
  } else {
69
    inji.startCallbacks();
70
  }
71
}
72
Inji.prototype.loadScripts = function (scripts, key) {
73
  this.addScript(scripts[key], function () {
74
    if (typeof (scripts[key].name) != 'undefined') {
75
      inji.loadedScripts[scripts[key].file] = true;
76
      if (typeof inji[scripts[key].name] == 'undefined') {
77
        console.log('js ' + scripts[key].name + '(' + scripts[key].file + ') loaded');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
78
        inji[scripts[key].name] = new window[scripts[key].name]();
79
        if (typeof (inji[scripts[key].name].init) == 'function') {
80
          inji[scripts[key].name].init();
81
        }
82
      }
83
    } else if (typeof (scripts[key].file) != 'undefined') {
84
      inji.loadedScripts[scripts[key].file] = true;
85
      console.log('js ' + scripts[key].file + ' loaded');
86
87
    } else {
88
      inji.loadedScripts[scripts[key]] = true;
89
      console.log('js ' + scripts[key] + ' loaded');
90
      inji.event('loadScript', scripts[key]);
91
    }
92
    if (typeof (scripts[key + 1]) != 'undefined') {
93
      inji.loadScripts(scripts, key + 1);
94
    } else {
95
      console.log('All scripts loaded');
96
      inji.startCallbacks();
97
    }
98
  });
99
}
100
Inji.prototype.addScript = function (script, callback) {
101
  var element = document.createElement('script');
102
  var src = '';
103
  if (typeof (script.file) != 'undefined') {
104
    src = script.file;
105
  } else {
106
    src = script;
107
  }
108
  if (inji.loadedScripts[src]) {
109
    if (typeof (callback) == 'function') {
110
      callback();
111
    }
112
    return true;
113
  }
114
  element.src = src;
115
  element.type = 'text/javascript';
116
  if (typeof (callback) == 'function') {
117
    element.onload = callback;
118
  }
119
  document.head.appendChild(element);
120
121
122
}
123
Inji.prototype.on = function (eventType, callback) {
124
  if (typeof this.listeners[eventType] == 'undefined') {
125
    this.listeners[eventType] = [];
126
  }
127
  this.listeners[eventType].push(callback);
128
}
129
Inji.prototype.event = function (eventType, object) {
130
  if (typeof this.listeners[eventType] != 'undefined') {
131
    for (key in this.listeners[eventType]) {
0 ignored issues
show
Bug introduced by
The variable key seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.key.
Loading history...
132
      this.listeners[eventType][key](eventType, object);
133
    }
134
  }
135
}
136
Inji.prototype.randomString = function (length) {
137
  if (!length) {
138
    length = 20;
139
  }
140
  var text = "";
141
  var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
142
143
  for (var i = 0; i < length; i++)
144
    text += chars.charAt(Math.floor(Math.random() * chars.length));
145
146
  return text;
147
}
148
Inji.prototype.numberFormat = function (number, decimals, dec_point, thousands_sep) {
149
  //// Format a number with grouped thousands
150
  // 
151
  // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
152
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
153
  // +	 bugfix by: Michael White (http://crestidg.com)
154
155
  var i, j, kw, kd, km;
156
157
  // input sanitation & defaults
158
  if (isNaN(decimals = Math.abs(decimals))) {
159
    decimals = 2;
160
  }
161
  if (dec_point == undefined) {
162
    dec_point = ",";
163
  }
164
  if (thousands_sep == undefined) {
165
    thousands_sep = ".";
166
  }
167
168
  i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
169
170
  if ((j = i.length) > 3) {
171
    j = j % 3;
172
  } else {
173
    j = 0;
174
  }
175
176
  km = (j ? i.substr(0, j) + thousands_sep : "");
177
  kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
178
  //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
179
  kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
180
181
182
  return km + kw + kd;
183
}
184
Inji.prototype.get = function (query) {
185
  var element = document.querySelector(query);
186
  if (element) {
187
    return new function () {
188
      this.element = element;
189
      this.attr = function (name) {
190
        for (var key in this.element.attributes) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
191
          var attr = element.attributes[key];
192
          if (attr.name == name) {
193
            return attr.value;
194
          }
195
        }
196
        return null;
197
      }
198
      this.data = function (name) {
199
        var data = this.attr('data-' + name);
200
        try {
201
          return JSON.parse(data);
202
        } catch (e) {
203
          return data;
204
        }
205
206
      }
207
    }
208
  }
209
}
210
var inji = new Inji();